package HelperFinancial.DataBase;

import HelperFinancial.Class.Categorias;
import HelperFinancial.Class.Credito;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CategoriaHelper extends SQLiteOpenHelper {
	
	private static final String DATABASE_NAME="categorias.db";
	private static final int SCHEMA_VERSION=1;
	
	public CategoriaHelper(Context context) {
		super(context, DATABASE_NAME, null, SCHEMA_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE categoria (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");
	}
	
	public void insertCategory(String categoria) {
		ContentValues cv = new ContentValues();
		cv.put("name", categoria);
		getWritableDatabase().insert("categoria", "name", cv);
	}
	
	public Cursor getAllCategory() {
		return(getReadableDatabase()
		.rawQuery("SELECT * FROM categoria",
		null));
	}
	
	public Cursor getCategoryByName(String name){
		return(getReadableDatabase()
				.rawQuery("SELECT * FROM categoria WHERE name = '" + name + "'",
				null));
	}
	
	public Cursor getCategoryById(Integer id){
		return(getReadableDatabase()
				.rawQuery("SELECT * FROM categoria WHERE _id = " + id,
				null));
	}
	
	
	public void removeCategory(String name){
		getReadableDatabase().delete("categoria", "name= '" + name + "'", null);
	}
	
	public void populateDataBase() {
		for (int i = 0; i < Categorias.values().length; i++) {
			insertCategory(((Categorias)Categorias.values()[i]).getName());
		}
		
	}
	
	public void removeAllCategorys() {
		getReadableDatabase().delete("categoria", null, null);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	

	
	
}
